package com.sales.service.thread;

import com.sales.service.constants.ServiceConstants;

import android.util.Log;

/**
 * This is the class of name ServiceThreadPool.
 * The singleton design pattern is used to have a single instance of the class
 * at any point of time and to have a global access to it.
 * The Singleton Design Pattern is done by 4 steps.
 *   one: The constructor is made as private.
 *   two: A public level method getInstance() is declared as static.
 * three: The public static method is made synchronised for thread safe.
 *  four: A private static modifier is declared.
 * Thus Singleton Design is achieved.
 * Inside the method the instance is initiated.
 * The thread class is called.
 * 
 * @author Sega
 *
 */
public class ServiceThreadPool {

	/**
	 * Strings for debugging process by log.i method
	 */
	private static final String TagProject = "SalesService";
	private static final String TagPage = "ThreadPool ::";

	/**
	 * private static instance for Singleton class.
	 */
	private static ServiceThreadPool instance = null;
	
	/**
	 * ServiceThread class instance. 
	 */
	private ServiceThread serviceThread[];

	/**
	 * private constructor for singleton design.
	 */
	private ServiceThreadPool() {
	}

	/**
	 * getInstance() method is the globally accessible method for getting the
	 * instance of this singleton class. This method provide the instance of 
	 * this class if it is null and if it is not null it provides the available
	 * instance. Then the MultiThreading() method is called to declare the size
	 * of thread array and to start all the threads.
	 * 
	 * @return - Returns the instance of this singleton class.
	 */
	public static synchronized ServiceThreadPool getInstance() {
		if (instance == null) {
			instance = new ServiceThreadPool();
		}
		instance.MultiThreading();
		return instance;
	}

	/**
	 * The thread size is assigned and all the thread are started by calling the servicethread class
	 */
	private void MultiThreading() {
		Log.i(TagProject, TagPage + "MultiThreading started.");
		serviceThread = new ServiceThread[ServiceConstants.MAX_NO_THREAD_IN_THREADPOOL];

		for (int threadId = 0; threadId < ServiceConstants.MAX_NO_THREAD_IN_THREADPOOL; threadId++) {
			serviceThread[threadId] = new ServiceThread(threadId);
			serviceThread[threadId].start();
		}
	}

}